package mhuu.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mhuu.constant.AppConfig;
import mhuu.service.SendHttpService;

public class ButtonPanel extends JPanel {

	public ButtonPanel() {
		this.add(jLabel);
		this.add(jTextField);
		this.add(jButtonFaSong);
		this.add(jButton);
		this.add(jButtonContentType);
		jButtonFaSong.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						SendHttpService.sendHttp();

					}
				}

				).start();

			}
		});
		jButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String requestString = HttpRequestPanel.jTextArea.getText();
				if (requestString.contains("GET ")) {
					requestString = requestString.replaceAll("GET ", "POST ");
				} else if (requestString.contains("POST ")) {
					requestString = requestString.replaceAll("POST ", "GET ");
				} 

				HttpRequestPanel.jTextArea.setText(requestString);
				MainWindow.mainPanel.setVisible(true);
			}
		});
		jButtonContentType.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String requestString = HttpRequestPanel.jTextArea.getText();
				if (requestString.contains("application/x-www-form-urlencoded")) {
					requestString = requestString.replaceAll("application/x-www-form-urlencoded", "application/json");
				} else if (requestString.contains("application/json")) {
					requestString = requestString.replaceAll("application/json", "application/x-www-form-urlencoded");
				}

				HttpRequestPanel.jTextArea.setText(requestString);
				MainWindow.mainPanel.setVisible(true);
			}
		});
	}

	private JLabel jLabel = new JLabel("请求地址：");
	public static JTextField jTextField = new JTextField(45);
	{
		jTextField.setText("http://127.0.0.1:9000");
	}
	private JButton jButtonFaSong = new JButton("发送");
	private JButton jButton = new JButton("切换请求方法");
	private JButton jButtonContentType = new JButton("切换ContentType");
}
